<template>
  <div>
    <table-custom
      ref="tableCustomRef"
      pageType="tps-warehouse"
      :api="getWarehouseList"
      :option="option"
      :searchInfo="searchInfo"
    >
      <!-- 左侧搜索 -->
      <!-- <template #tableTitle>
        <search-com
          :shippingType="dicData.tps_shipping_type"
          :statusArr="dicData.tps_shipping_order_status"
          @handle-search="onSearch"
        />
      </template> -->

      <!-- 右侧按钮 -->
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新建</a-button>
      </template>

      <!-- 操作栏插槽 -->
      <template #menuBtn="{ record }">
        <!-- <btn-com :record="record" /> -->
        <a-button type="link" @click="openDrawer(true, record)">详情</a-button>
        <Popconfirm
          title="确定要删除该仓库数据？"
          ok-text="确定"
          cancel-text="取消"
          @confirm="handleDel(record.id)"
        >
          <a-button type="link">删除</a-button>
        </Popconfirm>
      </template>

      <!-- 每列数据插槽 -->
      <!-- <template #bodyCell="{ column, record }"> </template> -->
    </table-custom>

    <!-- 详情弹窗 -->
    <DetailModal @register="register" @refresh="onSearch" />

    <!-- 新增仓库 -->
    <createDialog @register="registerCreateDriver" @reload="onSearch" />
  </div>
</template>

<script lang="ts" setup>
  import TableCustom from '@/components/TableCustom/TableCustom.vue';
  import { ref, nextTick } from 'vue';
  import DetailModal from './components/DetailModal.vue';
  import { useDrawer } from '@/components/Drawer';
  import { useModal } from '@/components/Modal';
  import createDialog from './components/createDialog/index.vue';
  import { deleteWarehouseInfo, getWarehouseList } from '@/api/warehouse';
  import { useMessage } from '@/hooks/web/useMessage';
  import { Popconfirm } from 'ant-design-vue';
  import { getFormConfig } from '@/views/shipper/warehouse/searchSchemas';

  const [register, { openDrawer }] = useDrawer();
  const [registerCreateDriver, { openModal: createDriverUseModal }] = useModal();

  const { createMessage } = useMessage();
  const tableCustomRef = ref(null);
  const selectLists: any = ref([]);
  const searchInfo = ref({});
  const deliveryType = ref('1'); // 运单类型：1-送货，2-退货

  // 获取外部传入的订单类型
  const props = defineProps({
    orderType: {
      type: String,
      default: '1',
    },
  });
  if (props.orderType) deliveryType.value = props.orderType;
  searchInfo.value = {
    deliveryType: deliveryType.value,
  };

  console.log('searchInfo', searchInfo.value);
  // 配置表格数据
  const option = ref({
    useSearchForm: true,
    formConfig: {
      ...getFormConfig(),
      defaultValue: {},
    },
  });

  // 搜索
  function onSearch(params: any) {
    searchInfo.value = params;

    // 清空已选项
    selectLists.value = [];
    // 获取表格组件实例
    const comRef: any = tableCustomRef.value;
    nextTick(() => {
      comRef.handleSearch({ searchInfo: { ...params } });
    });
  }

  function handleCreate() {
    createDriverUseModal(true, {});
  }

  function handleDel(id: string) {
    deleteWarehouseInfo(id).then(() => {
      createMessage.success('删除成功');
      onSearch(searchInfo.value);
    });
  }
</script>

<style lang="less" scoped></style>
